﻿Imports System.Guid
Public Class Client_CTRL
    Implements IDisposable


    Private _listeClient As List(Of Clients)
    Private _index As Integer = 0
    Private _clientCourant As Clients
    Private _EDM As AniFormEntities



#Region "Cycle de vie du controleur CLIENT"

    ''' <summary>
    ''' Constructeur du controleur CLIENT (on lui passe l'EDM)
    ''' </summary>
    ''' <param name="EDM"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal EDM As AniFormEntities)

        _EDM = EDM

        _listeClient = New List(Of Clients)

        getAllClients()

    End Sub

    Private disposedValue As Boolean = False        ' Pour détecter les appels redondants

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then

            End If

            For Each client In _listeClient
                client = Nothing
            Next
            _listeClient.Clear()
            _listeClient = Nothing
            _EDM.Dispose()
            _clientCourant = Nothing

        End If
        Me.disposedValue = True
    End Sub
    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

#End Region

    ''' <summary>
    ''' Propriete retournant le client courant
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property clientCourant() As Clients
        Get
            If _listeClient.Count > 0 Then
                _clientCourant = _listeClient(_index)
                Return _clientCourant
            Else
                Return Nothing
            End If
        End Get
    End Property

    ''' <summary>
    ''' Propriete d'index de navigation permetant de le modifier / recuperer
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property index() As Integer
        Get
            Return _index
        End Get
        Set(ByVal value As Integer)
            _index = value
        End Set
    End Property

    ''' <summary>
    ''' Propriete retournant la liste de client courante (peut changer en mode recherche)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property listeClient() As IEnumerable(Of Clients)
        Get
            Return _listeClient
        End Get
    End Property

    ''' <summary>
    ''' Procedure de recherche d'un client par son nom
    ''' </summary>
    ''' <param name="nom"></param>
    ''' <remarks></remarks>
    Public Sub rechercheClient(ByVal nom As String)

        Dim requete As IEnumerable(Of Clients) = From unclient In _EDM.Clients Select unclient Where unclient.NomClient.Contains(nom) And unclient.Archive = False Distinct

        _listeClient.Clear()
        _listeClient.AddRange(requete)

        If _listeClient.Count > 0 Then
            _index = 0
            _clientCourant = _listeClient(_index)
        Else
            _index = 0
            _clientCourant = Nothing
        End If

    End Sub

    ''' <summary>
    ''' Procedure remplaçant la listeclient actuelle par la liste complete des clients à partir de l'EDM
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub getAllClients()

        Dim requete As IEnumerable = (From unClient In _EDM.Clients Select unClient Where unClient.Archive = False)

        _listeClient.Clear()
        _listeClient.AddRange(requete)

        _clientCourant = _listeClient(0)

    End Sub

    ''' <summary>
    ''' Procedure d'ajout d'un nouveau client
    ''' </summary>
    ''' <param name="nom"></param>
    ''' <param name="prenom"></param>
    ''' <param name="adresse1"></param>
    ''' <param name="adresse2"></param>
    ''' <param name="ville"></param>
    ''' <param name="codePostal"></param>
    ''' <param name="telephone"></param>
    ''' <param name="assurance"></param>
    ''' <param name="email"></param>
    ''' <remarks></remarks>
    Public Sub ajoutClient(ByVal nom As String, ByVal prenom As String, ByVal adresse1 As String, ByVal adresse2 As String, ByVal ville As String, ByVal codePostal As String, ByVal telephone As String, ByVal assurance As String, ByVal email As String)

        Dim clientAajouter As New Clients

        clientAajouter.CodeClient = NewGuid()
        clientAajouter.NomClient = nom
        clientAajouter.PrenomClient = prenom
        clientAajouter.Adresse1 = adresse1
        clientAajouter.Adresse2 = adresse2
        clientAajouter.Ville = ville
        clientAajouter.CodePostal = codePostal
        clientAajouter.NumTel = telephone
        clientAajouter.Assurance = assurance
        clientAajouter.Email = email

        _listeClient.Add(clientAajouter)
        _EDM.AddToClients(clientAajouter)

        _EDM.SaveChanges(True)
        _clientCourant = clientAajouter
    End Sub

    ''' <summary>
    ''' Procedure de modification du client courant (nom,prenom,adresse,ville,codepostal,telephone,assurance,email)
    ''' </summary>
    ''' <param name="nom"></param>
    ''' <param name="prenom"></param>
    ''' <param name="adresse1"></param>
    ''' <param name="adresse2"></param>
    ''' <param name="ville"></param>
    ''' <param name="codePostal"></param>
    ''' <param name="telephone"></param>
    ''' <param name="assurance"></param>
    ''' <param name="email"></param>
    ''' <remarks></remarks>
    Public Sub modifClient(ByVal nom As String, ByVal prenom As String, ByVal adresse1 As String, ByVal adresse2 As String, ByVal ville As String, ByVal codePostal As String, ByVal telephone As String, ByVal assurance As String, ByVal email As String)

        _clientCourant.modifierClient(nom, prenom, adresse1, adresse2, ville, codePostal, telephone, assurance, email)
        _EDM.SaveChanges(True)

    End Sub

    ''' <summary>
    ''' Procedure d'achivage du client et de ses animaux ainsi que ses factures et consultation
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub archiverClient()

        'archivage du clientCourant
        _clientCourant.archiverClient()
        _listeClient.Remove(_clientCourant)
        _EDM.SaveChanges(True)

    End Sub



End Class
